<?php
exit();
if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
$ui = new UI();
include( "admin_menu.php" );
$conf = new Config();
if ( $conf->get( "calcbet" ) == 1 )
{
    $ui->showErrorPage( "<p class=error>已经在颁奖过帐中，无法删除注单！</p>" );
    exit();
}
if ( isset( $_POST['id'] ) )
{
    $id = intval( $_POST['id'] );
    if ( $id == 0 )
    {
        $ui->showErrorPage( "错误的注单编号！" );
        exit();
    }
    $bet = intval( $_POST['bet'] );
    $betname = "";
    if ( $bet == 1 )
    {
        $betfile = "bet";
        $betname = "会员注单";
    }
    else if ( $bet == 2 )
    {
        $betfile = "bet2";
        $betname = "下线走飞注单";
    }
    else if ( $bet == 3 )
    {
        $betfile = "bet8";
        $betname = "公司走飞注单";
    }
    else if ( $bet == 4 )
    {
        $betfile = "bet_all";
        $betname = "管理层全部注单";
    }
    else
    {
        $ui->showErrorPage( "请选择注单类型！" );
        exit();
    }
    if ( $betfile == "bet_all" )
    {
        $id = getvar( "id" );
        $rs = getdb( "select id,acl from k_user where account='".$id."'" );
        if ( $rs->eof )
        {
            $ui->showErrorPage( "错误的会员帐号！" );
            exit();
        }
        $userid = $rs->f['id'];
        $acl = $rs->f['acl'];
        if ( $acl == 1 )
        {
            $userlist = $userid;
        }
        else
        {
            $userlist = GetUserList( $userid );
            if ( $userlist == "" )
            {
                $ui->showErrorPage( "没有会员！" );
                exit();
            }
        }
        $rs = getdb( "select id,open,round from lotto where open>='".date( "Y-m-d" )."' order by open" );
        $lotto_id = $rs->f['id'];
        $open = $rs->f['open'];
        $round = $rs->f['round'];
        $rs->close();
        $rs = getdb( "select count(id) from bet where lotto_id=".$lotto_id." and user_id in (".$userlist.")" );
        if ( $rs->f[0] == 0 )
        {
            $ui->showErrorPage( "没有任何注单！" );
            exit();
        }
        $betcount = $rs->f[0];
        if ( isset( $_POST['ok'] ) )
        {
            getdb( "delete from  bet where lotto_id=".$lotto_id." and user_id in (".$userlist.") and rame in (0,2)" );
            if ( 1 < $acl )
            {
                getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id=".$userid." and rame in (0,2)" );
                if ( $acl == 3 )
                {
                    $userlist2 = GetUserList( $userid, 2 );
                    if ( $userlist2 != "" )
                    {
                        getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id in (".$userlist2.") and rame in (0,2)" );
                    }
                }
                else if ( $acl == 4 )
                {
                    $userlist2 = GetUserList( $userid, 2 );
                    $userlist3 = GetUserList( $userid, 3 );
                    if ( $userlist2 != "" )
                    {
                        getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id in (".$userlist2.") and rame in (0,2)" );
                    }
                    if ( $userlist3 != "" )
                    {
                        getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id in (".$userlist3.") and rame in (0,2)" );
                    }
                }
                else if ( $acl == 5 )
                {
                    $userlist2 = GetUserList( $userid, 2 );
                    $userlist3 = GetUserList( $userid, 3 );
                    $userlist4 = GetUserList( $userid, 4 );
                    if ( $userlist2 != "" )
                    {
                        getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id in (".$userlist2.") and rame in (0,2)" );
                    }
                    if ( $userlist3 != "" )
                    {
                        getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id in (".$userlist3.") and rame in (0,2)" );
                    }
                    if ( $userlist4 != "" )
                    {
                        getdb( "delete from  bet2 where lotto_id=".$lotto_id." and user_id in (".$userlist4.") and rame in (0,2)" );
                    }
                }
            }
            echo "<p>注单全部取消完成!</p>";
        }
        else
        {
            $name = "会员";
            if ( $acl == 2 )
            {
                $name = "代理";
            }
            else if ( $acl == 3 )
            {
                $name = "总代理";
            }
            else if ( $acl == 4 )
            {
                $name = "股东";
            }
            else if ( $acl == 5 )
            {
                $name = "大股东";
            }
            echo "<form action=\"op.php?op=admin&fp=delbet1\" method=post onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
            echo "<table border=1 cellpadding=3 cellspacing=0>";
            echo "<tr><td class=td0>期数<td>";
            echo d3( $round )." 期 <font color=#217952>".$open."</font>";
            echo "<tr><td class=td0>会员下注笔数<td  align=right>".$betcount."</table>";
            echo "<p>确定取消这位".$name." ".$id."所属全部会员的注单？</p>";
            echo "<input type=hidden name=id value={$id}>";
            echo "<input type=hidden name=bet value={$bet}>";
            echo "<input type=hidden name=ok value=1>";
            echo "<p><input type=submit value=确定> <input type=button value=放弃 onclick=\"location.href='op.php?op=admin&fp=delbet1'\">";
            echo "</form>";
        }
    }
    else
    {
        $id = intval( $_POST['id'] );
        $rs = getdb( "select * from ".$betfile." where id=".$id );
        if ( $rs->eof )
        {
            $ui->showErrorPage( "错误的注单编号！" );
            exit();
        }
        if ( 0 < $rs->f['rame'] )
        {
            $ui->showErrorPage( "已经颁奖完毕注单无法删除！" );
            exit();
        }
        if ( isset( $_POST['ok'] ) )
        {
            if ( 0 < $rs->f['rame'] )
            {
                echo "已经颁奖完毕注单无法删除！";
            }
            else
            {
                getdb( "delete from ".$betfile." where id=".$id." and rame=0" );
                echo "<p>删除注单完成!</p>";
            }
        }
        else
        {
            echo "<font color=red><b>删除 ".$betname."</b></font><br>";
            echo "<form action=\"op.php?op=admin&fp=delbet\" method=post onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
            echo "<table border=1 cellpadding=3 cellspacing=0>";
            echo "<tr><td class=td0>注单编号<td>".$id;
            if ( $bet < 3 )
            {
                $u = get_user( $rs->f['user_id'] );
                if ( $bet == 1 )
                {
                    echo "<tr><td class=td0>会员帐号<td>".$u['account'];
                }
                else
                {
                    $acl = $u['acl'];
                    $name = "";
                    if ( $acl == 2 )
                    {
                        $name = "代理";
                    }
                    else if ( $acl == 3 )
                    {
                        $name = "总代理";
                    }
                    else if ( $acl == 4 )
                    {
                        $name = "股东";
                    }
                    else if ( $acl == 5 )
                    {
                        $name = "大股东";
                    }
                    echo "<tr><td class=td0>".$name."帐号<td>".$u['account'];
                }
            }
            echo "<tr><td class=td0>时间<td>".date( "m-d H:i:s", $rs->f['createtime'] );
            echo "<tr><td class=td0>期数<td>";
            $lotto = new lotto( $rs->f['lotto_id'] );
            echo d3( $lotto->data['round'] )." 期 <font color=#217952>".$lotto->data['open']."</font>";
            echo "<tr><td class=td0>注单内容<td>".GetBetContent( $rs->f['bettype'], $rs->f['numbers'], $rs->f['amount'] );
            echo "<tr><td class=td0>赔率<td>".show_rate( $rs->f['bettype'], $rs->f['rate'] );
            if ( $bet == 3 )
            {
                echo "<tr><td class=td0>退水<td>".$rs->f['back'];
            }
            echo "<tr><td class=td0>金额<td>".$rs->f['amount'];
            echo "</table>";
            $stopbet = $conf->get( "waitbet" );
            $t = $lotto->data['open']." ".substr( $stopbet, 0, 2 ).":".substr( $stopbet, 2 );
            $t = strtotime( $t );
            $n = $lotto->data['n1'] + $lotto->data['n2'] + $lotto->data['n3'] + $lotto->data['n4'] + $lotto->data['n5'] + $lotto->data['n6'] + $lotto->data['spec'];
            if ( $conf->get( "calcbet" ) == 1 )
            {
                echo "<p><font color=red>已经在颁奖过帐中，无法删除注单！</font></p>";
            }
            else if ( $t <= systime( ) || 0 < $n )
            {
                echo "<p><font color=red>超过截止下注时间，无法删除该笔注单！</font></p>";
            }
            else
            {
                echo "<p>确定删除这笔 ".$betname."？</p>";
                echo "<input type=hidden name=id value={$id}>";
                echo "<input type=hidden name=bet value={$bet}>";
                echo "<input type=hidden name=ok value=1>";
                echo "<p><input type=submit value=确定> <input type=button value=取消 onclick=\"location.href='op.php?op=admin&fp=delbet'\">";
            }
            echo "</form>";
        }
    }
}
else
{
    echo "<font color=red><b>删除注单</b></font><br>";
    echo "<form action=\"op.php?op=admin&fp=delbet\" method=post onsubmit=\"this.submit.disabled=true;this.reset.disabled=true\">";
    echo "<input type=radio name=bet value=1 checked>会员注单&#12288;<input type=radio name=bet value=2>下线走飞注单&#12288;<input type=radio name=bet value=3>公司走飞注单&#12288;<input type=radio name=bet value=4>管理层全部注单<br><br>";
    echo "请输入注单编号：<input name=id>";
    echo "<input type=submit value=确定>";
    echo "</form>";
}
?>